package com.qingke.pms.command.system;

import org.apache.commons.codec.digest.DigestUtils;

import com.qingke.pms.app.ProjectManagementSystemApplication;
import com.qingke.pms.command.SystemCommand;
import com.qingke.pms.pojo.User;
import com.qingke.pms.util.PMSConsole;

public class LoginCommand extends SystemCommand{

	@Override
	public void execute() {
		PMSConsole.println();
		PMSConsole.println("----------------");
		String username=PMSConsole.promptNotEmpty("username:");
		String password=PMSConsole.promptNotEmpty("password:");
		PMSConsole.println("----------------");
		
		if(!dao.isUsernameExists(username)){
			PMSConsole.println("The user does not exist");
			return ;
		}
		
		User user=dao.login(username, DigestUtils.md5Hex(password));
		
		if(user!=null){
			ProjectManagementSystemApplication.getInstance().setUser(user);
			PMSConsole.println("Login success.");
			PMSConsole.println();
		}
		else{
			PMSConsole.println("Login failed , password is not correct");
			PMSConsole.println();
		}
	}

}
